package old;

import controller.IController;
import eventManager.AppEvent;
import utilities.Key;

public abstract class BaseEventTrap {
	
	//trap listens for events from this target
	public Key trapTarget;
	
	//the particular recipient of the information contained in the trap
	public Object trapRecipient;
	
	public IController controller;
	
	public int counter;
	//Once counter = threshold, trap executes
	public int counterThreshold;
	
	public BaseEventTrap(Key trapTarget, Object trapRecipient, IController controller, int counterThreshold) {
		this.trapTarget = trapTarget;
		this.trapRecipient = trapRecipient;
		this.controller = controller;
		this.counterThreshold = counterThreshold;
		counter = 0;
	}
	
	public void addData(AppEvent event) {
		boolean execSuccess = false;
		
		//boolean return requires processEvent() to finish and return a value before proceeding.  Keeps things orderly.
		boolean procSuccess = processEvent(event);
		
		if (counter >= counterThreshold) {
			execSuccess = executeTrap();
		}
		
		if (execSuccess) {
			controller.removeTrap(this);
		}
	}
	
	//processes event:  Updates data, adds to counter
	protected abstract boolean processEvent(AppEvent event);
	
	//contains methods using data to update the recipient
	protected abstract boolean executeTrap();
	
	

}
